package com.lzq.rabbitmqdemo.java_api.simple;

import com.lzq.rabbitmqdemo.java_api.constant.ConnectionConfig;
import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

public class Consumer {
    private final static String QUEUE_NAME = "hello";

    public static void main(String[] argv) throws Exception {
        ConnectionFactory factory = ConnectionConfig.getFactory();
        try (Connection connection = factory.newConnection();
             Channel channel = connection.createChannel()) {
            channel.queueDeclare(QUEUE_NAME, false, false, false, null);
            DefaultConsumer consumer = new DefaultConsumer(channel) {
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("接收到消息: " + new String(body));
                }
            };
            channel.basicConsume(QUEUE_NAME, true, consumer);
            TimeUnit.SECONDS.sleep(5);
        }
    }
}
